package com.antovita.militarymonkey;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

import com.antovita.militarymonkey.screens.AboutMenuScreen;
import com.antovita.militarymonkey.screens.GameScreen;
import com.antovita.militarymonkey.screens.LoadScreen;
import com.antovita.militarymonkey.screens.MenuScreen;
import com.antovita.militarymonkey.screens.PauseMenuScreen;
import com.antovita.militarymonkey.screens.QuitMenuScreen;
import com.antovita.militarymonkey.screens.ScoresMenuScreen;
import com.antovita.militarymonkey.screens.Screen;
import com.antovita.militarymonkey.screens.StartMenuScreen;
import com.antovita.militarymonkey.service.Action;
import com.antovita.militarymonkey.service.MusicBox;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;

public class MyGdxGame extends Task implements ApplicationListener {
	
	public FileHandle scores;
	public GameScreen game;
	public StartMenuScreen startScreen;
	public PauseMenuScreen pauseScreen;
	public AboutMenuScreen aboutScreen;
	public ScoresMenuScreen scoreScreen;
	public QuitMenuScreen quitScreen;
	private Screen screen;
	
	public void writeScore(long score) {
		scores.writeString(score + "\r\n", true);
	}
	
	public List<Integer> getScores() {
		Scanner scoreReader = new Scanner(scores.read());
		List<Integer> list = new ArrayList<Integer>();
		while (scoreReader.hasNextInt()) {
			list.add(scoreReader.nextInt());
		}
		scoreReader.close();
		Collections.sort(list);
		Collections.reverse(list);
		return list;
	}
	
	public void setStartScreen() {
		MenuScreen.start = true;
		if (screen == game) {
			MusicBox.getInstance().stopMusic();
			MusicBox.getInstance().play(Action.MENU);
		}
		screen = startScreen;
		screen.setInput();
	}
	
	public void setPauseScreen() {

		MenuScreen.start = false;
		screen = pauseScreen;
		screen.setInput();
	}
	
	public void startGame() {
		if (screen == startScreen) {
			MusicBox.getInstance().stopMusic();
			MusicBox.getInstance().play(Action.GAME);
		}
		if (MenuScreen.start) game = new GameScreen(this);
		MenuScreen.start = false;
		game.time.init();
		screen = game;
		screen.setInput();
	}
	
	public void quitGame() {
		dispose();
	}
	
	public void setQuitScreen() {
		screen = quitScreen;
		screen.setInput();
	}
	
	public void showScores() {
		scoreScreen.setScores(getScores());
		screen = scoreScreen;
		screen.setInput();
	}
	
	public void showAbout() {
		screen = aboutScreen;
		screen.setInput();
	}
	
	@Override
	public void create() {
		MusicBox.getInstance().play(Action.MENU);
		
		screen = new LoadScreen(this);
		(new Timer()).scheduleTask(this, 2);
		Gdx.input.setCatchBackKey(true);
	}

	@Override
	public void dispose() {
		Gdx.app.exit();
	}

	
	@Override
	public void render() {
		screen.render();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
		 if (!MenuScreen.start) setPauseScreen();
	}

	@Override
	public void resume() {
	}
	
	@Override
	public void run() {
		startScreen = new StartMenuScreen(this);
		pauseScreen = new PauseMenuScreen(this);
		aboutScreen = new AboutMenuScreen(this);
		scoreScreen = new ScoresMenuScreen(this);
		quitScreen = new QuitMenuScreen(this);
		setStartScreen();
		scores = Gdx.files.local("data/scores");
		if (!scores.exists()) scores.writeString("", true);
	}
}
